package com.qiaofang.collectmanage.stub.feign.client.impl;

import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.feign.client.ElementRuleClient;
import com.qiaofang.collectmanage.stub.request.*;
import com.qiaofang.collectmanage.stub.response.*;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.collectmanage.stub.facade.ElementRuleFacade;
import com.qiaofang.collectmanage.stub.request.GetElementRuleRequest;
import com.qiaofang.collectmanage.stub.request.ListElementRuleRequest;
import com.qiaofang.collectmanage.stub.request.SearchElementRulePageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("elementRuleClient")
public class ElementRuleClientImpl implements ElementRuleClient {

    @Autowired
    ElementRuleFacade elementRuleFacade;

    @Override
    public ElementRuleDTO getElementRuleById(Integer id) {
        GetElementRuleByIdRequest request = new GetElementRuleByIdRequest();
        request.setId(id);
        GetElementRuleByIdResponse response = elementRuleFacade.getElementRuleById(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getData();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public ElementRuleDTO getElementRule( GetElementRuleDTO getElementRuleDTO) {
        GetElementRuleRequest request = new GetElementRuleRequest();
        request.setGetElementRuleDTO(getElementRuleDTO);
        GetElementRuleResponse response = elementRuleFacade.getElementRule(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getData();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public void saveElementRule( SaveElementRuleDTO saveElementRuleDTO) {
        SaveElementRuleRequest request = new SaveElementRuleRequest();
        request.setSaveElementRuleDTO(saveElementRuleDTO);
        CommonResponse response = elementRuleFacade.saveElementRule(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public void updateElementRule(UpdateElementRuleDTO updateElementRuleDTO) {
        UpdateElementRuleRequest request = new UpdateElementRuleRequest();
        request.setUpdateElementRuleDTO(updateElementRuleDTO);
        CommonResponse response = elementRuleFacade.updateElementRule(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public void removeElementRule( RemoveElementRuleDTO removeElementRuleDTO) {
        RemoveElementRuleRequest request = new RemoveElementRuleRequest();
        request.setRemoveElementRuleDTO(removeElementRuleDTO);
        CommonResponse response = elementRuleFacade.removeElementRule(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public List<ElementRuleDTO> listElementRule(ListElementRuleDTO elementRuleDTO) {
        ListElementRuleRequest request = new ListElementRuleRequest();
        request.setElementRuleDTO(elementRuleDTO);
        ListElementRuleResponse response = elementRuleFacade.listElementRule(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getData();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public SearchElementRulePageDTO searchElementRulePage(PageDTO pageDTO, SearchElementRulePostDTO searchElementRulePostDTO) {
        SearchElementRulePageRequest request = new SearchElementRulePageRequest();
        request.setPageDTO(pageDTO);
        request.setSearchElementRulePostDTO(searchElementRulePostDTO);
        SearchElementRulePageResponse response = elementRuleFacade.searchElementRulePage(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getSearchElementRulePageDTO();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public List<TestFieldsByIdDTO> testFieldsById(List<Integer> ids) {
        TestFieldsByIdRequest request = new TestFieldsByIdRequest();
        request.setIds(ids);
        TestFieldsByIdResponse response = elementRuleFacade.testFieldsById(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getData();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public List<TestFieldsByIdDTO> testToolFieldsById(List<Integer> ids) {
        TestToolFieldsByIdRequest request = new TestToolFieldsByIdRequest();
        request.setIds(ids);
        TestFieldsByIdResponse response = elementRuleFacade.testToolFieldsById(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getData();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }


    @Override
    public GetFieldsDTO getFields( String collectRuleUuid) {
        GetFieldsRequest request = new GetFieldsRequest();
        request.setCollectRuleUuid(collectRuleUuid);
        GetFieldsResponse response = elementRuleFacade.getFields(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getGetFieldsDTO();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }


}
